package com.example.educationcommunity.controller;

import com.example.educationcommunity.dto.CommentDTO;
import com.example.educationcommunity.dto.QuestionDTO;
import com.example.educationcommunity.mapper.UserMapper;
import com.example.educationcommunity.model.Comment;
import com.example.educationcommunity.model.User;
import com.example.educationcommunity.service.CommentService;
import com.example.educationcommunity.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/question/{id}")
    public String question(@PathVariable(name = "id") String id,
            Model model){
        QuestionDTO questionDTO = questionService.getById(Integer.parseInt(id));
        //查看回复数量↓
        questionDTO.setCommentCount(commentService.countByParentId(Integer.parseInt(id)));
        model.addAttribute("questionId",id);
        model.addAttribute("question",questionDTO);
        questionService.incView(id);
        //按照ID寻找问题↑
        List<CommentDTO> commentDTOList = commentService.getByParentIdList(Integer.parseInt(id));
        model.addAttribute("comments",commentDTOList);
        //System.out.println(commentDTOList.get(0).getLikeCount());

        return "question";
    }
    //id是question的Id

    @PostMapping ("/question/{id}")
    public String doComment(@PathVariable(name = "id") String id,
                            @RequestParam("comment") String comment,
                            Model  model,
                            HttpServletRequest request){
        if (comment == null || comment == "") {
            model.addAttribute("error", "回复的内容不能为空");
            return "question";
        }

        User user = (User) request.getSession().getAttribute("user");

        if (user == null) {
            model.addAttribute("error", "用户未登陆");
            return "question";
        }

        Comment newComment = new Comment();
        newComment.setContent(comment);
        newComment.setCommentator(user.getId());
        newComment.setParentId(Integer.parseInt(id));
        newComment.setLikeCount(0);

        commentService.createOrUpdate(newComment);
        QuestionDTO questionDTO = questionService.getById(Integer.parseInt(id));
        //查看回复数量↓
        questionDTO.setCommentCount(commentService.countByParentId(Integer.parseInt(id)));
        model.addAttribute("questionId",id);
        model.addAttribute("question",questionDTO);
        //按照ID寻找问题↑
        List<CommentDTO> commentDTOList = commentService.getByParentIdList(Integer.parseInt(id));
        model.addAttribute("comments",commentDTOList);
        if(user.getQuestionCount()==null) {
            user.setQuestionCount(0);
        }
        if(user.getCommentCount()==null) {
            user.setCommentCount(0);
        }
        if(user.getActive()==null) {
            user.setActive(0);
        }
        user.setCommentCount(user.getCommentCount()+1);
        user.setActive(user.getActive()+3);
        userMapper.updateByPrimaryKey(user);

        return "question";
        }

}

